(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.companyClass.createController', createController);
	createController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'storageService', 
			'companyClassService' ];
	function createController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, storageService, companyClassService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		var log = $scope.loggerFactory.getLogger("companyClassCreate");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.init = function() {
			ctrl.companyClass = {};
			ctrl.companyClass.siteCode = storageService.getUserInfo().siteCode;
			authService.create(params).then(function(response) {
				ctrl.companyClass = response.data || params;
			});
		};

		ctrl.setComClassCode = function(){
			if (ctrl.companyClass && ctrl.companyClass.companyClassName != null){
				ctrl.companyClass.siteCode = storageService.getUserInfo().siteCode;
				ctrl.companyClass.companyClass = ctrl.companyClass.siteCode + ":" + ctrl.companyClass.companyClassName;
			} else {
				if ( ctrl.companyClass)
				ctrl.companyClass.companyClass = null;
			}
		};
		
		ctrl.createSave = function() {
			ctrl.companyClass.companyClass = ctrl.companyClass.companyClass.replace(/\s+/g,"");
			if (ctrl.companyClass.taskId) {
				return authService.createContinueSave(ctrl.companyClass).then(saveSuccessful, saveFailed);
			} else {
				log.debug('prop in companyClass:');
				for (var name in ctrl.companyClass) {
					log.debug(name + '=' + ctrl.companyClass[name]);
				}
				return authService.createSave(ctrl.companyClass).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			ctrl.companyClass.companyClass = ctrl.companyClass.companyClass.replace(/\s+/g,"");
			if (ctrl.companyClass.taskId) {
				return authService.createContinueSubmit(ctrl.companyClass).then(submitSuccessful, submitFailed);
			} else {
				return authService.createSubmit(ctrl.companyClass).then(submitSuccessful, submitFailed);
			}
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.companyClass.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.companyclass.list',
					'state' : 'cda.companyClass.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.companyClass = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);